package com.nccsjz.back.customer.service;

import java.sql.SQLException;
import java.util.List;

import com.nccsjz.base.BaseService;
import com.nccsjz.pojo.Customer;
import com.nccsjz.pojo.Merchant;
import com.nccsjz.pojo.News;
import com.nccsjz.back.customer.dao.CustomerDAO;
import com.nccsjz.back.merch.dao.MerchDAO;
import com.nccsjz.back.news.dao.NewsDAO;
import com.nccsjz.utils.Pager;

/**
 * 用于处理商户管理相关的业务逻辑 ＠author stone
 * 
 * @version 1.0
 * */
public class CustomerService extends BaseService {
	// 根据主键查看一条卡户
	public Customer getCus(long id) throws SQLException {
		Customer cus = null;
		try {
			openCon();
			cus = new CustomerDAO(con).getCus(id);
		} finally {
			closeCon();
		}
		return cus;
	}

	// 添加一条商户
	public boolean addCus(Customer cus) throws SQLException {
		boolean flag = false;
		try {
			openCon();
			flag = new CustomerDAO(con).addCus(cus);
		} finally {
			closeCon();
		}
		return flag;
	}

	// 删除一条商户
	public boolean deleteCus(long id) throws SQLException {
		boolean flag = false;
		try {
			openCon();
			flag = new CustomerDAO(con).deleteCus(id);

		} finally {
			closeCon();
		}
		return flag;
	}

	// 分有处理商户
	public Pager getCusList(int pageSize, int pageNo) throws SQLException {
		Pager pager = null;
		try {
			openCon();
			pager = new CustomerDAO(con).getCusList(pageSize, pageNo);
		} finally {
			closeCon();
		}
		return pager;
	}

	// 修改商户
	public boolean updateMerch(Merchant merch) throws SQLException {
		boolean flag = false;
		try {
			openCon();
			flag = new MerchDAO(con).updateMerch(merch);
		} catch (Exception e) {
			e.printStackTrace();
		}

		finally {
			closeCon();
		}
		return flag;
	}

	// 批量删除商户
	public boolean deleteBatchNews(List<Long> list) throws SQLException {
		boolean flag = false;
		try {
			openCon();
			con.setAutoCommit(false);
			flag = new NewsDAO(con).deleteBathNews(list);
			con.commit();
		} finally {
			closeCon();
		}
		return flag;
	}

	// add by changpf
	/**
	 * 得到前n条商户。
	 * 
	 * @return
	 */
	public List<Merchant> getTopN(int n) {
		List<Merchant> merchs = null;
		try {
			openCon();
			merchs = new MerchDAO(con).getTopN(n);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				closeCon();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return merchs;
	}

	/**
	 * 根据ID得到商户信息。
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Merchant getMerchById(long id) throws Exception {
		this.openCon();
		Merchant merch = new MerchDAO(con).getMerch(id);
		this.closeCon();
		return merch;
	}

	/**
	 * 根据 name,pwd取得卡户
	 * 
	 * @param name
	 * @param pwd
	 * @return
	 * @throws SQLException
	 */
	public Customer getCusByNameAndPwd(String name, String pwd)
			throws SQLException {
		Customer cus=new Customer();
		this.openCon();
		cus=new CustomerDAO(con).getCusByNameAndPwd(name, pwd);
		this.closeCon();
		return cus;

	}

	public boolean updateCus(Customer merch) {
		boolean f=false;
		try {
			this.openCon();
			f=new CustomerDAO(con).update(merch);
			this.closeCon();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return f;
	}

	public Pager getCusList(int pageSize, int pageNo, Customer merch) throws SQLException {
		Pager pager = null;
		try {
			openCon();
			pager = new CustomerDAO(con).getCusList(pageSize, pageNo,merch);
		} finally {
			closeCon();
		}
		return pager;
	}
	
	/**
	 * 检查注册的卡名是否已经存在
	 * 
	 * @param username
	 * @param type
	 * @return
	 * @throws SQLException 
	 */
	public boolean isExists(String username) throws SQLException{
		boolean flag = false;
		try{
			openCon();
			flag = new CustomerDAO(con).isCustExistsByName(username);
		}finally{
			closeCon();
		}
		return flag;
	}

	/**
	 * initCustPwd 方法根据卡户编号初始化卡户密码
	 * @param id
	 * @throws SQLException 
	 */
	public void initCustPwd(Long id) throws SQLException{
		try{
			openCon();
			new CustomerDAO(con).initPwdById(id);
		}finally{
			closeCon();
		}
	}

}
